/**
 *   This file is part of Java Simple Sampler.
 *
 *   Java Simple Sampler is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License.
 *
 *   Java Simple Sampler is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Java Simple Sampler.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   @author David Lapetina	
 */

package net.onirik.mao.sampler;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;

import net.onirik.mao.sampler.midi.PlayerMidiListener;
import net.onirik.mao.sampler.sound.WavSamplePlayer;

/**
 * Dummy class for dummy tests only.
 * @author david
 *
 */
public class Dummy {
	public static class MyReceiver extends Object implements Receiver {
		MidiDevice device;

		public MyReceiver(MidiDevice device) {
			this.device = device;

		}

		public void send(MidiMessage msg, long time) {
			System.out.println("Received message " + msg + " on "
					+ device.toString());
			if (ShortMessage.class.isInstance(msg)) {
				ShortMessage message = (ShortMessage) msg;
				int channel = message.getChannel();
				int command = message.getCommand();
				int data1 = message.getData1();
				int data2 = message.getData2();
				int status = message.getStatus();
				System.out.println("command" + command);
				switch (command) {
				case ShortMessage.NOTE_ON : System.out.println("note on" + command + " data 1" + data1 + " data2 " + data2);
											new PlayerMidiListener(new WavSamplePlayer("sounds/info.wav")											).onEvent();
											break;
				case ShortMessage.NOTE_OFF : System.out.println("note off" + command);
				break;											
				}
			}
		}

		public void close() {
			System.out.println("Closing");
		}
	}

	private static MidiDevice getDevice() throws MidiUnavailableException {
		MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();
		for (int i = 0; i < info.length; i++) {
			MidiDevice device = MidiSystem.getMidiDevice(info[i]);
			if (info[i].getName().indexOf("VirMIDI") > -1) {
				System.out.println("Connect to :");
				System.out.println(i + ") " + info[i]);
				System.out.println("Name: " + info[i].getName());
				System.out.println("Description: " + info[i].getDescription());
				return device;
			}
		}
		return null;
	}

	public static void main(String[] a) throws Exception {
		MidiDevice device = getDevice();
		try {
			device.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			device.getTransmitter().setReceiver(new MyReceiver(device));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.in.read();
		try {
			device.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
